package com.wiretap.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.wiretap.dao.RemoteSmsDao;
import com.wiretap.po.RemoteSms;
import com.wiretap.util.Pager;

public class RemoteSmsDaoImpl implements RemoteSmsDao {
	private SessionFactory sessionFactory;

	@Override
	public void addRemoteSms(RemoteSms sms) throws Exception {
		try {
			sessionFactory.getCurrentSession().persist(sms);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("addRemoteSms发生异常");
		}
	}

	@Override
	public int deleteRemoteSms(String id) throws Exception {
		try {
			return sessionFactory.getCurrentSession().createQuery("delete from RemoteSms r where r.id=:id").setString("id", id).executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("deleteRemoteSms发生异常");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pager<RemoteSms> getContactRemoteSms(String ownerPhone, String contactPhone, int currentPage, int pageSize) throws Exception {
		try {
			Session sesion = this.sessionFactory.getCurrentSession();
			Query query = sesion.createQuery("select count(*) from RemoteSms a where a.ownerPhone =:ownerPhone and a.targetPhone=:targetPhone").setString("ownerPhone", ownerPhone)
					.setString("targetPhone", contactPhone);
			int totalCount = ((Number) query.uniqueResult()).intValue();
			int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;
			List<RemoteSms> list = sesion.createQuery("from RemoteSms a where a.ownerPhone =:ownerPhone and a.targetPhone=:targetPhone order by a.addDate desc").setString("ownerPhone", ownerPhone)
					.setString("targetPhone", contactPhone).setFirstResult((currentPage - 1) * pageSize).setMaxResults(pageSize).list();
			Pager<RemoteSms> pager = new Pager<RemoteSms>(list, currentPage, totalCount, pageCount);
			return pager;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("getContactRemoteSms发生异常");
		}
	}

	@Override
	public void updateRemoteSms(RemoteSms sms) throws Exception {
		try {
			sessionFactory.getCurrentSession().update(sms);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("updateRemoteSms发生异常");
		}
	}

	@Override
	public RemoteSms getRemoteSmsById(String id) throws Exception {
		try {
			return (RemoteSms) sessionFactory.getCurrentSession().get(RemoteSms.class, id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("getRemoteSmsById发生异常");
		}
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public int updateRemoteSmsExecutedStatus(String[] ids, int status) throws Exception {
		try {
			String hql = "update RemoteSms a set a.status=? , a.excuted=? where ";
			for (int i = 0; i < ids.length; i++) {
				hql += "a.id=?";
				if (i != ids.length - 1) {
					hql += " or ";
				}
			}
			Query query = sessionFactory.getCurrentSession().createQuery(hql);
			query.setInteger(0, status);
			query.setBoolean(1, true);
			for (int i = 0; i < ids.length; i++) {
				query.setString(i + 2, ids[i]);
			}
			return query.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("updateRemoteSmsExecutedStatus发生异常");
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<RemoteSms> getAllUnexecutedRemoteSms(String ownerPhone) throws Exception {
		Session sesion = this.sessionFactory.getCurrentSession();
		List<RemoteSms> list = sesion.createQuery("from RemoteSms a where a.ownerPhone = :ownerPhone and a.excuted = :excuted").setString("ownerPhone", ownerPhone).setBoolean("excuted", false).list();
		return list;
	}

	@Override
	public int deleteRemoteSmsByOwnerPhone(String ownerPhone) throws Exception {
		try {
			return sessionFactory.getCurrentSession().createQuery("delete from RemoteSms a where a.ownerPhone = :ownerPhone ").setString("ownerPhone", ownerPhone).executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("deleteRemoteSmsByOwnerPhone发生异常");
		}
	}
}
